<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `diff_identifier` (
	`diff_identifierid` int(11) NOT NULL auto_increment,
	`tag` VARCHAR(255) NOT NULL, PRIMARY KEY  (`diff_identifierid`)) ENGINE=MyISAM;
*/

/**
* <b>diff_identifier</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5&wrapper=pog&objectName=diff_identifier&attributeList=array+%28%0A++0+%3D%3E+%27tag%27%2C%0A++1+%3D%3E+%27diff_record%27%2C%0A%29&typeList=array+%28%0A++0+%3D%3E+%27VARCHAR%28255%29%27%2C%0A++1+%3D%3E+%27HASMANY%27%2C%0A%29
*/
include_once('class.pog_base.php');
class diff_identifier extends POG_Base
{
	public $diff_identifierId = '';

	/**
	 * @var VARCHAR(255)
	 */
	public $tag;
	
	/**
	 * @var private array of diff_record objects
	 */
	private $_diff_recordList = array();
	
	public $pog_attribute_type = array(
		"diff_identifierId" => array('db_attributes' => array("NUMERIC", "INT")),
		"tag" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"diff_record" => array('db_attributes' => array("OBJECT", "HASMANY")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function diff_identifier($tag='')
	{
		$this->tag = $tag;
		$this->_diff_recordList = array();
	}
	
	
	/**
	* Gets object from database
	* @param integer $diff_identifierId 
	* @return object $diff_identifier
	*/
	function Get($diff_identifierId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `diff_identifier` where `diff_identifierid`='".intval($diff_identifierId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->diff_identifierId = $row['diff_identifierid'];
			$this->tag = $this->Unescape($row['tag']);
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $diff_identifierList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `diff_identifier` ";
		$diff_identifierList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "diff_identifierid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$diff_identifier = new $thisObjectName();
			$diff_identifier->diff_identifierId = $row['diff_identifierid'];
			$diff_identifier->tag = $this->Unescape($row['tag']);
			$diff_identifierList[] = $diff_identifier;
		}
		return $diff_identifierList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $diff_identifierId
	*/
	function Save($deep = true)
	{
		$connection = Database::Connect();
		$this->pog_query = "select `diff_identifierid` from `diff_identifier` where `diff_identifierid`='".$this->diff_identifierId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `diff_identifier` set 
			`tag`='".$this->Escape($this->tag)."'where `diff_identifierid`='".$this->diff_identifierId."'";
		}
		else
		{
			$this->pog_query = "insert into `diff_identifier` (`tag`) values (
			'".$this->Escape($this->tag)."')";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->diff_identifierId == "")
		{
			$this->diff_identifierId = $insertId;
		}
		if ($deep)
		{
			foreach ($this->_diff_recordList as $diff_record)
			{
				$diff_record->diff_identifierId = $this->diff_identifierId;
				$diff_record->Save($deep);
			}
		}
		return $this->diff_identifierId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $diff_identifierId
	*/
	function SaveNew($deep = false)
	{
		$this->diff_identifierId = '';
		return $this->Save($deep);
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete($deep = false, $across = false)
	{
		if ($deep)
		{
			$diff_recordList = $this->GetDiff_recordList();
			foreach ($diff_recordList as $diff_record)
			{
				$diff_record->Delete($deep, $across);
			}
		}
		$connection = Database::Connect();
		$this->pog_query = "delete from `diff_identifier` where `diff_identifierid`='".$this->diff_identifierId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array, $deep = false, $across = false)
	{
		if (sizeof($fcv_array) > 0)
		{
			if ($deep || $across)
			{
				$objectList = $this->GetList($fcv_array);
				foreach ($objectList as $object)
				{
					$object->Delete($deep, $across);
				}
			}
			else
			{
				$connection = Database::Connect();
				$pog_query = "delete from `diff_identifier` where ";
				for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
				{
					if (sizeof($fcv_array[$i]) == 1)
					{
						$pog_query .= " ".$fcv_array[$i][0]." ";
						continue;
					}
					else
					{
						if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
						{
							$pog_query .= " AND ";
						}
						if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
						}
						else
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
						}
					}
				}
				return Database::NonQuery($pog_query, $connection);
			}
		}
	}
	
	
	/**
	* Gets a list of diff_record objects associated to this one
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array of diff_record objects
	*/
	function GetDiff_recordList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$diff_record = new diff_record();
		$fcv_array[] = array("diff_identifierId", "=", $this->diff_identifierId);
		$dbObjects = $diff_record->GetList($fcv_array, $sortBy, $ascending, $limit);
		return $dbObjects;
	}
	
	
	/**
	* Makes this the parent of all diff_record objects in the diff_record List array. Any existing diff_record will become orphan(s)
	* @return null
	*/
	function SetDiff_recordList(&$list)
	{
		$this->_diff_recordList = array();
		$existingDiff_recordList = $this->GetDiff_recordList();
		foreach ($existingDiff_recordList as $diff_record)
		{
			$diff_record->diff_identifierId = '';
			$diff_record->Save(false);
		}
		$this->_diff_recordList = $list;
	}
	
	
	/**
	* Associates the diff_record object to this one
	* @return 
	*/
	function AddDiff_record(&$diff_record)
	{
		$diff_record->diff_identifierId = $this->diff_identifierId;
		$found = false;
		foreach($this->_diff_recordList as $diff_record2)
		{
			if ($diff_record->diff_recordId > 0 && $diff_record->diff_recordId == $diff_record2->diff_recordId)
			{
				$found = true;
				break;
			}
		}
		if (!$found)
		{
			$this->_diff_recordList[] = $diff_record;
		}
	}
}
?>